!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==================================================================================================================================|
subroutine read_parameters
!==================================================================================================================================|

   use parameters
   implicit none

   namelist /parameters_list/                                                                                                      &
      n_dims,                                                                                                                      &
      sys,                                                                                                                         &
      ng,                                                                                                                          &
      nx_no_bnd,                                                                                                                   &
      ini_block_x,                                                                                                                 &
      ny_no_bnd,                                                                                                                   &
      ini_block_y,                                                                                                                 &
      ratio,                                                                                                                       &
      n_levels,                                                                                                                    &
      n_neighs,                                                                                                                    &
      scheme,                                                                                                                      &
      riemann_solver_flag,                                                                                                         &
      regrid_iterative_flag,                                                                                                       &
      reflux_flag,                                                                                                                 &
      damping_flag,                                                                                                                &
      ini_value_save_flag,                                                                                                         &
      div_clean_flag,                                                                                                              &
      syn_check_flag,                                                                                                              &
      save_state_flag,                                                                                                             &
      dn_save_state,                                                                                                               &
      load_state_flag,                                                                                                             &
      n_load_state,                                                                                                                &
      output_real8_flag,                                                                                                           &
      interpolation_flag,                                                                                                          &
      gravity_reset_flag,                                                                                                          &
      fixed_mesh_flag,                                                                                                             &
      resistivity_flag,                                                                                                            &
      heat_conduction_flag,                                                                                                        &
      t_end,                                                                                                                       &
      dt_output,                                                                                                                   &
      cfl,                                                                                                                         &
      rangex,                                                                                                                      &
      zero_posx,                                                                                                                   &
      rangey,                                                                                                                      &
      zero_posy,                                                                                                                   &
      cd,                                                                                                                          &
      thr_al,                                                                                                                      &
      thr_de,                                                                                                                      &
      thr_ex,                                                                                                                      &
      thr_fl,                                                                                                                      &
      gm,                                                                                                                          &
      pi,                                                                                                                          &
      n_bnd_interior,                                                                                                              &
      datamap_path

   namelist /parameters_thresholds/                                                                                                &  
      thr_va0

   namelist /parameters_list_exter_boundary/                                                                                       &  
      bnd_exterior

   namelist /parameters_list_inter_boundary/                                                                                       &  
      bnd_interior_pos, bnd_interior

!----------------------------------------------------------------------------------------------------------------------------------|
   open (unit_par, file = trim (directory) // trim (parameters_filename))
   read (unit_par, nml = parameters_list)
   close (unit_par)

   allocate (thr_va0(n_thr))
   allocate (bnd_exterior(sys, n_neighs, n_dims))
   open (unit_par, file = trim (directory) // trim (parameters_filename))
   read (unit_par, nml = parameters_thresholds)
   close (unit_par)

   open (unit_par, file = trim (directory) // trim (parameters_filename))
   read (unit_par, nml = parameters_list_exter_boundary)
   close (unit_par)

   if (n_bnd_interior .gt. 0) then
      allocate (bnd_interior_pos(n_dims * n_neighs, n_bnd_interior))
      allocate (bnd_interior(sys, n_neighs, n_dims, n_bnd_interior))
      open (unit_par, file = trim (directory) // trim (parameters_filename))
      read (unit_par, nml = parameters_list_inter_boundary)
      close (unit_par)
   endif

   nx_block = nx_no_bnd + ng * 2
   nx_total = nx_no_bnd * ini_block_x + ng * 2
   ny_block = ny_no_bnd + ng * 2
   ny_total = ny_no_bnd * ini_block_y + ng * 2
   n_d_l = n_dims * n_levels
   ratio2 = ratio * ratio
   ini_block_order = ceiling (log(max(ini_block_x, ini_block_y) - 0.5) / log(2.0))
   ratio_order = ceiling (log(ratio - 0.5) / log(2.0))

   t_output = dt_output
   gmm1 = gm - 1.0d0
   eps = epsilon (1.0d0) * 1.0d-3
   hc0 = 0.0d0
   max_et = 0.0d0
   n_int = n_dims * n_levels + 1
   n_x = nx_block
   n_y = ny_block
   cd = cd * (rangex + rangey) / 2.0d0
   n_blocks_all = ini_block_x * ini_block_y
   n_blocks_vip_all = ini_block_x * ini_block_y

   nx_total_finest = nx_total * 2 ** (n_levels - 1)
   ny_total_finest = ny_total * 2 ** (n_levels - 1)

   if (damping_flag .eq. 1 .and. ini_value_save_flag .ne. 1 .and. cpu_id .eq. 0) then
      write(*, *) 'Warning: damping_flag = 1 require ini_value_save_flag = 1.'
      write(*, *) 'Warning: the value of ini_value_save_flag is changed to 1.'
      ini_value_save_flag = 1
   endif

!----------------------------------------------------------------------------------------------------------------------------------|
   return
end subroutine read_parameters
